package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.system.api.RemoteViewService;
import com.ruoyi.system.api.domain.Attachment;
import com.ruoyi.system.api.domain.AttachmentAssociated;
import com.ruoyi.utils.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AttachmentAssociatedMapper;
import com.ruoyi.system.service.IAttachmentAssociatedService;

/**
 * 附件关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-18
 */
@Service
public class AttachmentAssociatedServiceImpl implements IAttachmentAssociatedService 
{
    @Autowired
    private AttachmentAssociatedMapper attachmentAssociatedMapper;
    @Autowired
    private RemoteViewService remoteViewService;
    @Autowired
    private UploadFileService uploadFileService;
    /**
     * 查询附件关联
     * 
     * @param id 附件关联主键
     * @return 附件关联
     */
    @Override
    public AttachmentAssociated selectAttachmentAssociatedById(Long id)
    {
        return attachmentAssociatedMapper.selectAttachmentAssociatedById(id);
    }

    /**
     * 查询附件关联列表
     * 
     * @param attachmentAssociated 附件关联
     * @return 附件关联
     */
    @Override
    public List<AttachmentAssociated> selectAttachmentAssociatedList(AttachmentAssociated attachmentAssociated)
    {
        return attachmentAssociatedMapper.selectAttachmentAssociatedList(attachmentAssociated);
    }

    /**
     * 新增附件关联
     * 
     * @param attachmentAssociated 附件关联
     * @return 结果
     */
    @Override
    public int insertAttachmentAssociated(AttachmentAssociated attachmentAssociated)
    {
        attachmentAssociated.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        attachmentAssociated.setCreateTime(DateUtils.getNowDate());
        return attachmentAssociatedMapper.insertAttachmentAssociated(attachmentAssociated);
    }

    /**
     * 修改附件关联
     * 
     * @param attachmentAssociated 附件关联
     * @return 结果
     */
    @Override
    public int updateAttachmentAssociated(AttachmentAssociated attachmentAssociated)
    {
        return attachmentAssociatedMapper.updateAttachmentAssociated(attachmentAssociated);
    }

    /**
     * 批量删除附件关联
     * 
     * @param ids 需要删除的附件关联主键
     * @return 结果
     */
    @Override
    public int deleteAttachmentAssociatedByIds(Long[] ids)
    {
        return attachmentAssociatedMapper.deleteAttachmentAssociatedByIds(ids);
    }

    /**
     * 删除附件关联信息
     * 
     * @param id 附件关联主键
     * @return 结果
     */
    @Override
    public int deleteAttachmentAssociatedById(Long id)
    {
        return attachmentAssociatedMapper.deleteAttachmentAssociatedById(id);
    }
    /**
     * 获取附件filedId
     *
     * @param associatedId 附件关联主键
     * @return 结果
     */
    @Override
    public List<Attachment> getFiledIdByAssociatedId(Long associatedId)
    {
        return attachmentAssociatedMapper.getFileIdByAssociatedId(associatedId);
    }

    @Override
    public  List<String> viewByAssociatedId(Long associatedId) {
        List<Attachment> fileIds = attachmentAssociatedMapper.getFileIdByAssociatedId(associatedId);

        return fileIds.stream().map(fileId->uploadFileService.getPathUrl(fileId.getFileId(),fileId.getName())).collect(Collectors.toList());
    }
}
